﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZYLeGo2._0.Models;

namespace ZYLeGo2._0.Controllers
{
    public class AdminController : Controller
    {
        // GET: Admin
        //管理员登录
        public ActionResult Login()
        {
            return View();
        }
        [AdminAuthentication]
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(Admin admin)
        {
            if (ModelState.IsValid)
            {
                using (TradeDBEntities1 db = new TradeDBEntities1())
                {
                    var a = db.Admins.FirstOrDefault(x => x.aName == admin.aName && x.aPassword == admin.aPassword);
                    if (a == null)
                    {
                        ModelState.AddModelError("", "账号或密码错误！！！");
                    }
                    else
                    {
                        //登录验证
                        MyAuthentication.SetCookie(a.aName, a.aId.ToString(), "admin");
                        return RedirectToAction("Index",true);   //登陆成功
                    }
                }
            }
            return View();
        }
        //退出
        public ActionResult Logout()
        {
            MyAuthentication.LogOut();  //退出
            return RedirectToAction("Index");
        }
    }
}